package com.swedne.test.kt

import android.os.SystemPropertiesProto.Sys
import android.util.Log
import kotlinx.coroutines.*
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern


//fun main() {
//    runBlocking {
//        launch {
//            test()
//            println("jump test ${Thread.currentThread().name}")
//        }
//    }
//    println("main finish")
//}

suspend fun test() {
    println("test ${Thread.currentThread().name}")
    delay(2000)
    println("end work")

    GlobalScope.launch(Dispatchers.IO) { }
}


suspend fun add(a: Int, b: Int): Int {
    delay(1000L)
    return a + b
}

fun main() {



    /* Thread(Runnable {
         println("${Thread.currentThread().name}/// 1")
         runBlocking {
             println("${Thread.currentThread().name}/// 2")
             launch(Dispatchers.Default) { // 使用 Dispatchers.Default 启动独立协程
                 println("${Thread.currentThread().name}/// 3")
                 val result = async {
                     println("${Thread.currentThread().name}/// 4")
                     add()
                 }
                 result.await()
             }
 //            val async = async {
 //                println("${Thread.currentThread().name}/// 3")
 //                add()
 //            }
         }
     }
     ).start()*/

//    var startTime = System.currentTimeMillis()
//    runBlocking {
//        delay(1000)
//        println("===============1") // 立即输出
//
//    }
    runBlocking {
        val asyncJob1 = async {
            delay(5000L)
            println("=================1")
        }
        val asyncJob2 = async {
            delay(3000L)

            println("=================2")
        }
        val asyncJob3 = launch {
            delay(1000L)
            withContext(Dispatchers.IO) {
                delay(3000L)
                Thread {
                    Thread.sleep(1000L)
                    println("=================5///${Thread.currentThread().name}")

                }.start()
                println("=================4///${Thread.currentThread().name}")
            }
            println("=================3//${Thread.currentThread().name}")
        }
    }
    println("===========================")
    //这样写更好,才是并行效率高.输出的是2、1、0
//        asyncJob1.await()
//        asyncJob2.await()
//        println(System.currentTimeMillis() - startTime)
//        println("=============0")
//
//    }
//    CoroutineScope(Dispatchers.IO + CoroutineName("geee")).launch {
//
//    }
//
//    println("World2") // 立即输出
//    Thread.sleep(10 * 1000)

//    calculateTotalTimeMillis("8 Days 01:59:12")

}

suspend fun add() {
    delay(2000L)
    println("World") //
}



private fun calculateTotalTimeMillis(timeString: String): Long {
    val pattern = Pattern.compile("(\\d+) Days (\\d+):(\\d+):(\\d+)")
    val matcher = pattern.matcher(timeString)

    if (matcher.find()) {
        val days = matcher.group(1).toInt()
        val hours = matcher.group(2).toInt()
        val minutes = matcher.group(3).toInt()
        val seconds = matcher.group(4).toInt()
        println("days=${days}//${hours}//${minutes}//${seconds}")
        // 将时分秒转换为毫秒数
        return (days * 3600 * 24 + hours * 3600 + minutes * 60 + seconds) * 1000L
    }

    // 将 "X day HH:mm:ss" 格式的时间字符串解析为时间戳
    val indexOf = timeString.indexOf("Days")
    var days = ""
    if (indexOf != -1) {
        days = timeString.substring(0, indexOf).trim()
        println("days=${days}")
    }
//    val pattern = "'Days' HH:mm:ss"
//    val sdf = SimpleDateFormat(pattern, Locale.getDefault())
//
//    try {
//        val date = sdf.parse(timeString)
//        println("dateTime=${date?.time}")
//        val calendar = Calendar.getInstance()
//        calendar.clear()
//        date?.time?.let {
//            var temp=it
//            if (days.isNotEmpty()){
//                temp+=days.toInt()*60*60*1000
//            }
//            val seconds = it / 1000
//            val minutes = seconds / 60
//            val hours = minutes / 60
//            val days = hours / 24
//            println("///=${days}//${hours % 60}//${minutes % 60}//${seconds % 60}")
//        }
//
//
//        return date?.time ?: 0L
//    } catch (e: Exception) {
//        e.printStackTrace()
//    }

    return 0L
}

fun getString(str: String): String {
    return str
}